//
//  THJNetWork.m
//  THJXHW
//
//  Created by 河南德艺 on 17/3/11.
//  Copyright © 2017年 河南德艺. All rights reserved.
//

#import "THJNetWork.h"
#import <AFNetworking.h>

#import "ZBNetworking.h"

@implementation THJNetWork
+(void)getNetDataFromUrl:(NSString*)url callback:(NetHandler)handler{
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer.timeoutInterval = 13;
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager.requestSerializer setHTTPShouldHandleCookies:YES];
     NSLog(@"headersURL--%@", url);
    [manager GET:url parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    }
         success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
  
             NSLog(@"task---%@",responseObject);
             NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseObject options:kNilOptions error:nil];
             dispatch_async(dispatch_get_main_queue(), ^{
                 handler(json,nil);
             });
             
         }
         failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull   error) {
             
             dispatch_async(dispatch_get_main_queue(), ^{
                 handler(nil,error);
             });
             
         }];
}
+(void)getNetDataWithTokenFromUrl:(NSString*)url callback:(NetHandler)handler{
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer.timeoutInterval = 13;
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager.requestSerializer setHTTPShouldHandleCookies:YES];
     [manager.requestSerializer setValue:[THJTools getToken] forHTTPHeaderField:@"token"];
    NSLog(@"headersURL--%@", url);
    [manager GET:url parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    }
         success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
             
             NSLog(@"task---%@",responseObject);
             NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseObject options:kNilOptions error:nil];
             dispatch_async(dispatch_get_main_queue(), ^{
                 handler(json,nil);
             });
             
         }
         failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull   error) {
             
             dispatch_async(dispatch_get_main_queue(), ^{
                 handler(nil,error);
             });
             
         }];
}
+(void)getCompanyIntroFromUrl:(NSString*)url callback:(NetHandler)handler Type:(NSInteger)type{
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer.timeoutInterval = 13;
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
   // [manager.requestSerializer setHTTPShouldHandleCookies:YES];
    NSString* cookie = [[NSUserDefaults standardUserDefaults]objectForKey:@"cookies"];
    NSLog(@"COOKIES--%@",cookie);
    if (type == 1) {
        [manager.requestSerializer setValue:cookie forHTTPHeaderField:@"Cookie"];
    }else{
        NSString  *phpCookies = [NSString stringWithFormat:@"PHPSESSID=%@",cookie];
        [manager.requestSerializer setValue:phpCookies forHTTPHeaderField:@"Cookie"];
    }
    
    NSLog(@"获取公司介绍header--%@",manager.requestSerializer.HTTPRequestHeaders);
    [manager GET:url parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
    }
         success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
             
             //NSLog(@"task---%@-%@-%@",task.originalRequest,task.currentRequest,task.response);
             NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseObject options:kNilOptions error:nil];
             dispatch_async(dispatch_get_main_queue(), ^{
                 handler(json,nil);
             });
             
         }
     
         failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull   error) {
             
             dispatch_async(dispatch_get_main_queue(), ^{
                 handler(nil,error);
             });
             
         }];
}

+(void)postNetDataFromUrl:(NSString*)url parameters:(NSDictionary*)dic callback:(NetHandler)handler{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    NSDictionary *parameters = dic;
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager.requestSerializer setHTTPShouldHandleCookies:YES];
    manager.requestSerializer.timeoutInterval = 13;
 
    [manager POST:url parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
//        NSLog(@"888--:%@",responseObject);
        NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseObject options:kNilOptions error:nil];
        NSLog(@"responseObject--:%@",json);
        dispatch_async(dispatch_get_main_queue(), ^{
            handler(json,nil);
        });
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"777-error-%@",error);
        dispatch_async(dispatch_get_main_queue(), ^{
            handler(nil,error);
        });
    }];
}

+(void)postDataFromUrl:(NSString*)url parameters:(NSDictionary*)dic Token:(NSString*)token callback:(NetHandler)handler{
  
    
//    [ZBRequestManager requestWithConfig:^(ZBURLRequest *request){
//        request.urlString=url;
//        request.methodType = ZBMethodTypePOST;
//        request.apiType = ZBRequestTypeRefresh;
//        [request setValue:token forHeaderField:@"token"];
//        request.parameters = dic;
//    }  success:^(id responseObject,apiType type){
//        NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseObject options:kNilOptions error:nil];
//        //        NSString * status = json[@"status"];
//        //        if([status isEqual:@"200"]){
//        NSLog(@"flag-KK-%@-%@",responseObject,json);
//        handler(json,nil);
//        //        }
//    } failed:^(NSError *error){
//        handler(nil,error);
//        NSLog(@"error-%@",error);
//    }];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    NSDictionary * parameter = dic;
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager.requestSerializer setHTTPShouldHandleCookies:YES];
    manager.requestSerializer.timeoutInterval = 13;
    [manager.requestSerializer setValue:token forHTTPHeaderField:@"token"];
    [manager POST:url parameters:parameter progress:^(NSProgress * _Nonnull uploadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"tijiaodata-:%@",responseObject);
        NSString *result =[[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseObject options:kNilOptions error:nil];
        NSLog(@"tijiao-jso-%@-%@",json,result);
        dispatch_async(dispatch_get_main_queue(), ^{
            handler(json,nil);
        });

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        dispatch_async(dispatch_get_main_queue(), ^{
            handler(nil,error);
            NSLog(@"t---%@",error);
        });
    }];
}
+(void)postGroupAddNetDataFromUrl:(NSString*)url parameters:(NSDictionary*)dic callback:(NetHandler)handler  Type:(NSInteger)type{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    NSDictionary *parameters = dic;
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager.requestSerializer setHTTPShouldHandleCookies:YES];
    manager.requestSerializer.timeoutInterval = 13;
    NSString* cookie = [[NSUserDefaults standardUserDefaults]objectForKey:@"cookies"];
    NSLog(@"COOKIES--%@",cookie);
    if (type == 1) {
        [manager.requestSerializer setValue:cookie forHTTPHeaderField:@"Cookie"];
    }else{
        NSString  *phpCookies = [NSString stringWithFormat:@"PHPSESSID=%@",cookie];
        [manager.requestSerializer setValue:phpCookies forHTTPHeaderField:@"Cookie"];
    }
    [manager POST:url parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        // NSLog(@"responseObject:%@",responseObject);
        
        NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseObject options:kNilOptions error:nil];
        dispatch_async(dispatch_get_main_queue(), ^{
            handler(json,nil);
        });
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        dispatch_async(dispatch_get_main_queue(), ^{
            handler(nil,error);
        });
    }];
}
+(void)postLoginFromUrl:(NSString*)url parameters:(NSDictionary*)dic callback:(NetHandler)handler{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    NSDictionary *parameters = dic;
    NSLog(@"%@",url);
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager.requestSerializer setHTTPShouldHandleCookies:YES];
    manager.requestSerializer.timeoutInterval = 13;
//    NSString* cookie = [[NSUserDefaults standardUserDefaults]objectForKey:@"cookies"];
//    NSLog(@"COOKIES--%@",cookie);
//    if (type == 1) {
//        [manager.requestSerializer setValue:cookie forHTTPHeaderField:@"Cookie"];
//    }else{
//        NSString  *phpCookies = [NSString stringWithFormat:@"PHPSESSID=%@",cookie];
//        [manager.requestSerializer setValue:phpCookies forHTTPHeaderField:@"Cookie"];
//    }
    [manager POST:url parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
         NSLog(@"responseObject:%@",responseObject);
        NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseObject options:kNilOptions error:nil];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            handler(json,nil);
        });
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        dispatch_async(dispatch_get_main_queue(), ^{
            handler(nil,error);
            NSLog(@"error:%@",error);
        });
    }];
}

+(void)GetDataWithGetRequestTypeUrl:(NSString*)url Callback:(NetHandler)handler{
    [ZBRequestManager requestWithConfig:^(ZBURLRequest *request){
        request.urlString=url;
        request.apiType=ZBRequestTypeRefresh;
    }  success:^(id responseObject,apiType type){
       NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseObject options:kNilOptions error:nil];
//        NSString * status = json[@"status"];
//        if([status isEqual:@"200"]){
            NSLog(@"flag-KK-%@",responseObject);
            handler(json,nil);
//        }
    } failed:^(NSError *error){
        handler(nil,error);
    }];
}
+(void)GetProductInfoWithProductItemId:(NSString*)itemid Callback:(NetHandler)handler{
   NSString * url = [NSString stringWithFormat:@"%@%@%@&itemid=%@",THJUrlDomain,THJindexPHP,@"f=sell_5&r=sell_5&m=info",itemid];
    NSLog(@"产品详情url-%@",url);
    [ZBRequestManager requestWithConfig:^(ZBURLRequest *request){
        request.urlString=url;
        request.apiType=ZBRequestTypeCache;
    }  success:^(id responseObject,apiType type){
        NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseObject options:kNilOptions error:nil];
//        NSString * status = json[@"status"];
        //        if([status isEqual:@"200"]){
        NSLog(@"flag-KK");
        handler(json,nil);
        //        }
    } failed:^(NSError *error){
        handler(nil,error);
    }];
}
@end
